using System.IO;
using Newtonsoft.Json;

namespace AirCtrl.Wpf.Core;

/// <summary>
/// 相机、云台配置信息
/// </summary>
public class DevicesConfig : SingletonBase<DevicesConfig>
{
    public string AppName  { get; set; } ="国防科大采集软件";
    
    public List<string> CameraNames = new List<string>()
    {
        "长波热像仪", "偏振相机", "短波相机", "中波相机", "近红外相机", "近红外高光谱", "短波红外高光谱"
    };
    /// <summary>
    /// 是否需要云台控制
    /// </summary>
    public int NeedPtz { get; set; } =0;

    /// <summary>
    /// 云台COM口
    /// </summary>
    public string PtzCom { get; set; }
    public string SignalCom { get; set; }
    /// <summary>
    /// 相机标识
    /// key:DevicesManager.PropName; value:设备ID
    /// </summary>
    public Dictionary<string, string> CameraIds { get; set; } = new Dictionary<string, string>();
    /// <summary>
    /// 相机旋转角度
    /// key:camera.title; value:角度
    /// </summary>
    public Dictionary<string, int> CameraRotateAngle { get; set; } = new Dictionary<string, int>();
    /// <summary>
    /// 照片路径
    /// </summary>
    public string CapturePath { get; set; } = "\\Capture";
    /// <summary>
    /// 相机UDP端口
    /// key:CameraTitle/CameraDescription; value:端口号
    /// </summary>
    public Dictionary<string,int> CameraUdpPorts { get; set; } = new Dictionary<string, int>();
    public string CameraUdpIp { get; set; } = "127.0.0.1";

    /// <summary>
    /// 地空通讯-地面串口
    /// </summary>
    public string GroundClientCom { get; set; } = "COM61";
    /// <summary>
    /// 地空通讯-空中串口
    /// </summary>
    public string AirClientCom { get; set; } = "COM51";
    /// <summary>
    /// 可见光高光谱扫描重复文件路径
    /// </summary>
    public string GgpFilePath { get; set; } = "\\GGP\\ggp_file.txt";

    #region Methods

    /// <summary>
    /// 加载系统配置
    /// </summary>
    public void LoadConfig()
    {
        var path = GetConfigPath();
        try
        {
            var allText = File.ReadAllText(path);
            var obj = JsonConvert.DeserializeObject<DevicesConfig>(allText);

            foreach (var item in typeof(DevicesConfig).GetProperties())
            {
                item.SetValue(DevicesConfig.Instance, item.GetValue(obj));
            }

        }
        catch (Exception e)
        {
        }

        if (Instance.CameraUdpPorts.Count != CameraNames.Count)
        {
            var startPort = 10000;
            var i = 0;
            foreach (var camera in CameraNames)
            {
                if(CameraUdpPorts.ContainsKey(camera))
                    continue;

                i++;
                CameraUdpPorts.Add(camera, startPort + i);
            }
        }
    }

    /// <summary>
    /// 保存配置
    /// </summary>
    public void SaveConfig()
    {
        var path = GetConfigPath();
        var json = JsonConvert.SerializeObject(DevicesConfig.Instance);
        File.WriteAllText(path, json);
    }

    private string GetConfigPath()
    {
        var path = "";
        path = string.IsNullOrWhiteSpace(path) ? "./Config/Devices.config" : path;
        var dirName = System.IO.Path.GetDirectoryName(path);
        if (!string.IsNullOrWhiteSpace(dirName))
            Directory.CreateDirectory(Path.GetDirectoryName(path));
        return path;
    }

    #endregion
}